<!-- 以下代码为代码生成器生成，生成时间: 2017-02-20 14:15:46，作者：billjiang/475572229@qq.com-->
<section class="content-header">
    <h1>
        工作流模型
        <small>列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="${basePath}"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">工作流管理</a></li>
        <li class="active">工作流模型</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <!-- /.box-header -->
                <div class="dataTables_filter" id="searchDiv" style="display: none">
                    <!--查询条件-->
                    <input type="search" placeholder="请输入模型名称" title="名称" name="name"
                           class="form-control" id="name" operator="like" likeOption="true">
                    <div class="btn-group">
                        <button type="button" class="btn btn-primary" data-btn-type="search">查询</button>
                        <button type="button" class="btn btn-default" data-btn-type="reset">重置</button>
                    </div>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-btn-type="add">新增</button>
                        <button type="button" class="btn btn-default" data-btn-type="edit">编辑</button>
                        <button type="button" class="btn btn-default" data-btn-type="delete">删除</button>
                    </div>
                </div>
                <div class="box-body">
                    <table id="model_table" class="table table-bordered table-striped table-hover">
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div id="msgcontainer">

    </div>
</section>

<script>
    var modelTable, winId = "modelWin";

    var modelObj = {
        addModel: function () {
            modals.openWin({
                winId: winId,
                title: '新增工作流模型',
                width: '800px',
                url: basePath + "/activiti/model/edit/0"
            });
        },
        editModel: function (rowId) {
            if (!rowId) {
                modals.info('请选择要编辑的行');
                return false;
            }
            modals.openWin({
                winId: winId,
                title: '编辑工作流模型【' + modelTable.getSelectedRowData().name + '】',
                width: '800px',
                url: basePath + "/activiti/model/edit/" + rowId
            });
        },
        deleteModel: function (rowId) {
            if (!rowId) {
                modals.info('请选择要删除的行');
                return false;
            }
            modals.confirm("是否要删除该工作流模型？", function () {
                ajaxPost(basePath + "/activiti/model/delete/" + rowId, null, function (data) {
                    if (data.success) {
                        modelTable.reloadRowData();
                    } else {
                        modals.info("工作流模型被引用，不可删除！");
                    }
                });
            })

        },
        defineFlow: function (rowId) {
            window.open(basePath + "/activiti/modeler/" + rowId);
        },
        copyFlow:function (rowId) {
          ajaxPost(basePath+"/activiti/model/copy/"+rowId,null,function (data) {
              if(data.success){
                  modelTable.reloadRowData();
                  modals.correct("流程复制成功");
              }
          })
        },
        deployFlow: function (rowId) {
            var rowData = modelTable.getRowDataByRowId(rowId);
            if (!rowData.editorSourceExtraValueId) {
                modals.info("请先配置流程定义");
                return;
            }
            var msg = rowData.deploymentId ? "该流程已经部署过，是否重新部署？" : "是否部署该流程？";
            modals.confirm(msg, function () {
                ajaxPost(basePath + "/activiti/model/deploy/" + rowId, null, function (data) {
                    if (data.success) {
                        modals.correct("流程部署成功");
                        modelTable.reloadRowData(rowId);
                    } else {
                        modals.error(data.message);
                    }
                })
            })
        },
        showFlow: function (rowId) {
            ajaxPost(basePath + "/activiti/model/exist/xml/" + rowId, null, function (data) {
                if (data.success) {
                    var rowData = modelTable.getRowDataByRowId(rowId);
                    var title = "【" + rowData.name + "】xml&image";
                    modals.openWin({
                        winId: "flowWin",
                        width: "900px",
                        url: basePath + "/activiti/model/show/" + rowId,
                        title: title
                    });
                } else {
                    modals.error("该工作流模型的流程定义xml文件和image文件不存在，请在流程定义中绘制流程定义图");
                }
            })
        },
        exportXml: function (rowId) {
            ajaxPost(basePath + "/activiti/model/exist/xml/" + rowId, null, function (data) {
                if (data.success) {
                    window.location.href = basePath + "/activiti/model/export/xml/" + rowId;
                } else {
                    modals.error("该工作流模型的流程定义xml文件不存在，请在流程定义中绘制流程定义图");
                }
            })
        },
        exportImage: function (rowId) {
            ajaxPost(basePath + "/activiti/model/exist/xml/" + rowId, null, function (data) {
                if (data.success) {
                    window.location.href = basePath + "/activiti/model/export/image/" + rowId;
                } else {
                    modals.error("该工作流模型的流程定义png文件不存在，请在流程定义中绘制流程定义图");
                }
            })
        }

    }

    //方法入口
    $(function () {
        modelTable = new CommonTable("model_table", "model_list", "searchDiv", null);
        //绑定按钮事件
        $("button[data-btn-type]").click(function () {
            var action = $(this).data("btn-type");
            var rowId = modelTable.getSelectedRowId();
            switch (action) {
                case "add":
                    modelObj.addModel();
                    break;
                case "edit":
                    modelObj.editModel(rowId);
                    break;
                case 'delete':
                    modelObj.deleteModel(rowId);
                    break;
            }
        })
    })


    function fnRenderOperator(value, type, rowObj) {
        //流程定义
        var oper = "<a href='javascript:void(0)' onclick='modelObj.defineFlow(\"" + value + "\")'>建模</a>";
        oper += "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='modelObj.copyFlow(\"" + value + "\")'>复制</a>";
        //部署
        if (rowObj.editorSourceExtraValueId) {
            oper += "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='modelObj.deployFlow(\"" + value + "\")'>部署</a>";
            oper += "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='modelObj.showFlow(\"" + value + "\")'>查看</a>";
            oper += "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='modelObj.exportXml(\"" + value + "\")'>xml</a>";
            oper += "&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='modelObj.exportImage(\"" + value + "\")'>png</a>";
        } else {
            oper += "&nbsp;&nbsp;&nbsp;<span title='请先配置流程定义'>部署</span>";
            oper += "&nbsp;&nbsp;&nbsp;<span title='请先配置流程定义'>查看</span>";
            oper += "&nbsp;&nbsp;&nbsp;<span title='请先配置流程定义'>xml</span>";
            oper += "&nbsp;&nbsp;&nbsp;<span title='请先配置流程定义'>png</span>";
        }
        return oper;
    }
</script>